package com.sqx.modules.goods.controller;

import com.sqx.common.annotation.SysLog;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.goods.vo.GoodsVo;
import com.sqx.modules.goods.entity.Goods;
import com.sqx.modules.goods.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商品-管理端
 *
 * @author wuChang
 * @since 2024-05-30
 */
@RestController
@Api(value = "商品-管理端", tags = {"商品-管理端"})
@RequestMapping("/admin/goods/")
public class AdminGoodsController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 新增或修改商品
     *
     * @param goods 商品
     */
    @SysLog(value = "新增或修改商品", type = "1")
    @ApiOperation("新增或修改商品")
    @PostMapping(value = "saveGoods")
    public Result saveGoods(@ApiParam("商品的实体") @RequestBody Goods goods) {
        goods.setStatus(1);
        goods.setIsEnable(1);
        return goodsService.saveGoods(goods);
    }

    /**
     * 获取商品列表
     *
     * @param goodsVo 商品
     */
    @ApiOperation("获取商品列表")
    @GetMapping(value = "getAdminGoodsList")
    public Result getAdminGoodsList(@ApiParam("页码") Integer page,
                                    @ApiParam("条数") Integer limit,
                                    @ApiParam("商品的实体") GoodsVo goodsVo) {
        return Result.success().put("data", goodsService.getAdminGoodsList(page, limit, goodsVo));
    }

    /**
     * 获取回收商品列表
     * @param page
     * @param limit
     * @param goodsVo
     * @return
     */
    @ApiOperation("获取回收商品列表")
    @GetMapping(value = "getRecycleGoodsList")
    public Result getRecycleGoodsList(@ApiParam("页码") Integer page,
                                      @ApiParam("条数") Integer limit,
                                      @ApiParam("商品的实体") GoodsVo goodsVo) {
        return Result.success().put("data", goodsService.getAdminRecycleGoodsList(page, limit, goodsVo));
    }



    /**
     * 商品审核
     *
     * @param goodId     商品id
     * @param status     1审核通过 2审核未通过
     * @param authRemark 审核备注
     * @return
     */
    @ApiOperation("商品审核")
    @SysLog(value = "商品审核", type = "1")
    @GetMapping(value = "authGoods")
    public Result authGoods(@ApiParam("商品的id") Long goodId,
                            @ApiParam("1审核通过 2审核未通过") Integer status,
                            @ApiParam("审核备注") String authRemark) {
        return goodsService.authGoods(goodId, status, authRemark);
    }

    /**
     * 商品封禁
     *
     * @param goodId     商品id
     * @param authRemark 审核备注
     * @return
     */
    @ApiOperation("商品封禁")
    @SysLog(value = "商品封禁", type = "1")
    @GetMapping(value = "banGoods")
    public Result banGoods(@ApiParam("商品的id") Long goodId,
                           @ApiParam("审核备注") String authRemark) {
        return goodsService.banGoods(goodId, authRemark);
    }

    /**
     * 删除商品
     *
     * @param goodId 商品id
     */
    @ApiOperation("删除商品")
    @SysLog(value = "删除商品", type = "1")
    @PostMapping(value = "deleteGoodsById")
    public Result deleteGoodsById(@ApiParam("商品的id") Long goodId) {
        return goodsService.removeById(goodId) ? Result.success() : Result.error();
    }


    /**
     * 获取商品详情
     *
     * @param goodId 商品id
     */
    @ApiOperation("获取商品详情")
    @GetMapping(value = "getGoodsById")
    public Result getGoodsById(@ApiParam("商品的id") Long goodId) {
        return Result.success().put("data", goodsService.getGoodsById(goodId, null));
    }

    /**
     * 根据商品id获取规格详情
     *
     * @param goodId 商品id
     */
    @ApiOperation("根据商品id获取规格详情")
    @GetMapping(value = "getGoodsIdBySpec")
    public Result getGoodsIdBySpec(@ApiParam("商品的id") Long goodId) {
        return Result.success().put("data", goodsService.getGoodsIdBySpec(goodId));
    }


    /**
     * 商品上下架
     *
     * @param goods 商品的实体
     * @return
     */
    @ApiOperation("商品上下架")
    @SysLog(value = "商品上下架", type = "1")
    @GetMapping(value = "updateStatus")
    public Result updateStatus(@ApiParam("商品的实体") Goods goods) {
        return goodsService.updateStatusByGoodId(goods);
    }


    @ApiOperation("管理端选择是否置顶商品")
    @PostMapping(value = "adminIsTopGoods")
    public Result adminIsTopGoods(@ApiParam("商品id") Long goodId) {
        return goodsService.adminIsTopGoods(goodId);
    }
}